package class61To70;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;

// https://www.luogu.com.cn/problem/P3366
public class Prim {

    public static int n, m, u, v, w;

    public static final int MAX_N = 5001;

    public static final int MAX_M = 400005;

    public static int[] head = new int[MAX_N];

    public static int[] next = new int[MAX_M];

    public static int[] to = new int[MAX_M];

    public static int[] weight = new int[MAX_M];

    public static int cnt;

    public static void build(int n) {
        cnt = 1;
        Arrays.fill(head, 0, n + 1, 0);
    }

    public static void addEdge(int u, int v, int w) {
        next[cnt] = head[u];
        to[cnt] = v;
        weight[cnt] = w;
        head[u] = cnt++;
    }


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) in.nval;
            in.nextToken();
            int m = (int) in.nval;
            build(n);
            for (int i = 0; i < m; i++) {
                in.nextToken();
                int u = (int) in.nval;
                in.nextToken();
                int v = (int) in.nval;
                in.nextToken();
                int w = (int) in.nval;
                addEdge(u, v, w);
                addEdge(v, u, w);
            }
            PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
            boolean[] nodeSet = new boolean[n + 1];
            for (int ei = head[1]; ei > 0; ei = next[ei]) {
                pq.add(new int[]{to[ei], weight[ei]});
            }
            nodeSet[1] = true;
            int nodeCount = 1;
            int ans = 0;
            while (!pq.isEmpty()) {
                int[] edge = pq.poll();
                int toNode = edge[0];
                int nodeWeight = edge[1];
                if (!nodeSet[toNode]) {
                    ans += nodeWeight;
                    nodeCount++;
                    nodeSet[toNode] = true;
                    for (int ei = head[toNode]; ei > 0; ei = next[ei]) {
                        pq.add(new int[]{to[ei], weight[ei]});
                    }
                }
            }
            out.println(nodeCount == n ? ans : "orz");
        }
        out.flush();
        out.close();
        br.close();
    }
}
